package ui;

import server.HandleConn;
import server.Out;
import server.ServerMain;

import javax.swing.*;
import java.awt.*;
import java.util.Comparator;

public class Panel extends JPanel {
    static final Font font=new Font("等线",Font.BOLD,90),start=new Font("等线",Font.ITALIC,12)
            ,stf=new Font("等线",Font.BOLD,23);
    static final Color bg=new Color(230,230,230),st=new Color(70,170,230);
    public int idToDisplay=0;
    public String stateStr="点击按钮开始";
    int countDownTime=3;
    public Panel(){

    }
    public void sort(){
        ServerMain.checkConn.conns.sort(new Comparator<HandleConn>() {
            @Override
            public int compare(HandleConn o1, HandleConn o2) {
                if(o1.clientId>o2.clientId)
                    return 1;
                else if(o1.clientId<o2.clientId){
                    return -1;
                }
                return 0;
            }
        });
    }
    public void paint(Graphics g){
        g.setColor(bg);
        g.fillRect(0,0,this.getWidth(),this.getHeight());
        g.setFont(font);
        if(ServerMain.state==ServerMain.FREE) {//显示抢答到者id
            FontMetrics fontMetrics = g.getFontMetrics();
            int width = fontMetrics.stringWidth(idToDisplay==0?"无人抢答":""+idToDisplay);
            g.setColor(st);
            g.drawString(idToDisplay==0?"无人抢答":idToDisplay+"", this.getWidth() / 2 - width / 2, this.getHeight() / 2 - fontMetrics.getHeight() / 2);
        }else if(ServerMain.state==ServerMain.COUNT){
            g.setColor(Color.WHITE);
            ((Graphics2D)g).setStroke(new BasicStroke(3));
            g.drawRect((int)(this.getWidth()*0.6*0.5),(int)(this.getHeight()*0.2),(int)(this.getWidth()*0.4),(int)(this.getHeight()*0.05));
            g.setColor(Color.RED);
            g.fillRect((int)(this.getWidth()*0.6*0.5)+3,(int)(this.getHeight()*0.2)+3,(int)(this.getWidth()*0.4*0.333*this.countDownTime)-3,(int)(this.getHeight()*0.05)-6);

        }else if(ServerMain.state==ServerMain.RUSH){
            g.setColor(Color.red);
            g.setFont(start);
            //g.drawString("开始",(int)(this.getWidth()*0.6*0.5)+3,(int)(this.getHeight()*0.2)+3);
        }
        //状态字符串
        g.setColor(st);
        g.setFont(stf);

        FontMetrics fontMetrics = g.getFontMetrics();
        int width = fontMetrics.stringWidth(stateStr);
        g.drawString(this.stateStr,(int)(this.getWidth()*0.5-width/2), ServerMain.mf.sb.getY()+ServerMain.mf.sb.getHeight()/2+30);
        //Out.say("11111111111");
        //客户端列表
        sort();
        g.setColor(Color.orange);
        g.setFont(stf);

        FontMetrics fontMetrics0 = g.getFontMetrics();
        int width0 = fontMetrics0.stringWidth("已连接的手机");
        g.drawString("已连接的手机("+ServerMain.checkConn.conns.size()+")",(int)(this.getWidth()*0.5-width0/2), this.getHeight()-150);

        int amount=ServerMain.checkConn.conns.size();
        int w=30,h=40;
        int sep=20;
        int sx=this.getWidth()/2-amount*(w+sep)/2;
        for(int i=0;i<amount;i++){
            HandleConn hc=ServerMain.checkConn.conns.get(i);
            if(hc.rushable){//可冲
                g.setColor(st);
            }else {
                if(hc.rushtime==-2){//犯规
                    g.setColor(Color.RED);
                    g.drawString("犯规",sx+(w+sep)*i-6,this.getHeight()-127);
                }else {
                    g.setColor(st);
                }
            }
            //如果是被选中的
            if(hc.clientId==idToDisplay){
                g.setColor(Color.green);
            }
            g.fillRect(sx+(w+sep)*i,this.getHeight()-120,w,h);//
            g.setColor(Color.black);
            g.drawString(hc.clientId+"",sx+(w+sep)*i+5,this.getHeight()-60);
        }
    }
}
